package actions;

import modelo.Usuario;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import DAO.FactoryDAO;
import DAO.UsuarioDAO;

import com.opensymphony.xwork2.ActionSupport;

@Action(value = "/edit-grid-entry")
@Results({ @Result(location = "/jsp/principal/backend.jsp", name = "success"),
		@Result(location = "/jsp/principal/backend.jsp", name = "input") })
public class EditGridEntry extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(EditGridEntry.class);
	private Usuario user = new Usuario();
	private UsuarioDAO userDAO = FactoryDAO.getUsuarioDAO();

	private String id;
	private String name;
	private String clave;
	private String telefono;
	private String email;
	private String perfil;
	private String oper = "edit";

	public String execute() throws Exception {
		if (oper.equalsIgnoreCase("add")) {
			EditGridEntry.log.debug("Add Customer");
			user.setName(name);
			user.setClave(clave);
			user.setTelefono(telefono);
			user.setEmail(email);
			user.setPerfil(perfil);

			userDAO.guardar(user);

		} else if (oper.equalsIgnoreCase("edit")) {

			EditGridEntry.log.debug("Edit Usuario");
			user = userDAO.buscarUsuario(Long.parseLong(getId()));
			user.setName(name);
			user.setClave(clave);
			user.setTelefono(telefono);
			user.setEmail(email);
			user.setPerfil(perfil);

			userDAO.modificar(user);

		} else if (oper.equalsIgnoreCase("del")) {

			user = userDAO.buscarUsuario(Long.parseLong(getId()));
			userDAO.eliminar(user);
		}
		return SUCCESS;
	}

	

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getClave() {
		return clave;
	}

	public void setClave(String clave) {
		this.clave = clave;
	}

	public String getTelefono() {
		return telefono;
	}

	public void setTelefono(String telefono) {
		this.telefono = telefono;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPerfil() {
		return perfil;
	}

	public void setPerfil(String perfil) {
		this.perfil = perfil;
	}

}
